package soccer.view;

import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.JTextField;

public class PlayerStatisticPanel extends JPanel {
	private JTable mostGoalScorerTable;
	private JTextField playerName;
	private JTextField playerSurname;
	private JTable playedMatchTable;
	private JTable playerGoalMatchTable;
	private JButton getPlayerStatisticButton;
	private JButton backButtonOfPlayerStatictic;

	/**
	 * Create the panel.
	 */
	public PlayerStatisticPanel() {
		setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Most Goal Scorer");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(27, 34, 133, 14);
		add(lblNewLabel);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(27, 59, 155, 101);
		add(scrollPane);
		
		mostGoalScorerTable = new JTable();
		mostGoalScorerTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Player Name", "Goal Number"
			}
		));
		scrollPane.setViewportView(mostGoalScorerTable);
		
		getPlayerStatisticButton = new JButton("Get  a Player Statistic");
		getPlayerStatisticButton.setBounds(352, 119, 135, 23);
		add(getPlayerStatisticButton);
		
		playerName = new JTextField();
		playerName.setBounds(352, 57, 86, 20);
		add(playerName);
		playerName.setColumns(10);
		
		playerSurname = new JTextField();
		playerSurname.setBounds(352, 88, 86, 20);
		add(playerSurname);
		playerSurname.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Name :");
		lblNewLabel_1.setBounds(286, 60, 46, 14);
		add(lblNewLabel_1);
		
		JLabel lblSurname = new JLabel("Surname :");
		lblSurname.setBounds(286, 91, 56, 14);
		add(lblSurname);
		
		JLabel lblEnterNameAnd = new JLabel("Enter name and surname of player to get information");
		lblEnterNameAnd.setBounds(286, 34, 262, 14);
		add(lblEnterNameAnd);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 184, 262, 172);
		add(scrollPane_1);
		
		playedMatchTable = new JTable();
		playedMatchTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Date", "Home", "Skor", "Away"
			}
		));
		playedMatchTable.getColumnModel().getColumn(0).setPreferredWidth(72);
		playedMatchTable.getColumnModel().getColumn(2).setPreferredWidth(43);
		scrollPane_1.setViewportView(playedMatchTable);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(296, 184, 283, 172);
		add(scrollPane_2);
		
		playerGoalMatchTable = new JTable();
		playerGoalMatchTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Date", "Home", "Skor", "Away", "Minute"
			}
		));
		scrollPane_2.setViewportView(playerGoalMatchTable);
		
		JLabel lblMatchesThatPlayer = new JLabel("Matches that player played");
		lblMatchesThatPlayer.setHorizontalAlignment(SwingConstants.CENTER);
		lblMatchesThatPlayer.setBounds(22, 171, 202, 14);
		add(lblMatchesThatPlayer);
		
		JLabel lblNewLabel_2 = new JLabel("Matches that player have a goal");
		lblNewLabel_2.setBounds(340, 171, 166, 14);
		add(lblNewLabel_2);
		
		backButtonOfPlayerStatictic = new JButton("Back");
		backButtonOfPlayerStatictic.setBounds(232, 367, 89, 23);
		add(backButtonOfPlayerStatictic);

	}

	@SuppressWarnings("rawtypes")
	public void addGoalToMostGoalScorerTable(List scorerMapList){
		DefaultTableModel tableModel = (DefaultTableModel) mostGoalScorerTable.getModel();
		resetTeamTable(tableModel);
		for(int i = 0; i < 3; i++){
			Map scorerMap = (Map) scorerMapList.get(i);
			Vector<Comparable> v = new Vector<Comparable>();
			v.add((Comparable)( scorerMap.get("name").toString() 
					  + " " + scorerMap.get("surname").toString()));
			v.add((Comparable) scorerMap.get("goalNumber"));
	        tableModel.addRow(v);
		}
	}
	public void addPlayerToPlayerPlayedMatch(List matchList){
		DefaultTableModel tableModel = (DefaultTableModel) playedMatchTable.getModel();
		resetTeamTable(tableModel);
		for(int i = 0; i < matchList.size(); i++){
			Map matchMap = (Map) matchList.get(i);
			Vector<Comparable> v = new Vector<Comparable>();
			v.add((Comparable) matchMap.get("matchDate"));
			v.add((Comparable) matchMap.get("home"));
			v.add((Comparable) (matchMap.get("homeTeamSkor").toString()+
					":"+matchMap.get("awayTeamSkor").toString()));
			v.add((Comparable) matchMap.get("away"));
			tableModel.addRow(v);
		}
	}
	public void addPlayerToHaveGoalMatch(List matchList){
		DefaultTableModel tableModel = (DefaultTableModel) playerGoalMatchTable.getModel();
		resetTeamTable(tableModel);
		for(int i = 0; i < matchList.size(); i++){
			Map matchMap = (Map) matchList.get(i);
			Vector<Comparable> v = new Vector<Comparable>();
			v.add((Comparable) matchMap.get("matchDate"));
			v.add((Comparable) matchMap.get("home"));
			v.add((Comparable) (matchMap.get("homeTeamSkor").toString()+
					":"+matchMap.get("awayTeamSkor").toString()));
			v.add((Comparable) matchMap.get("away"));
			v.add((Comparable) matchMap.get("golminute"));
			tableModel.addRow(v);
		}
	}
	public void resetTeamTable(DefaultTableModel model){
		while(model.getRowCount() > 0){
			model.removeRow(0);
		}
	}
	public void addbackButtonActionListener(ActionListener l){
		backButtonOfPlayerStatictic.addActionListener(l);
	}
	public void addGetPlayerStatisticButtonActionListener(ActionListener l){
		getPlayerStatisticButton.addActionListener(l);
	}
	public JTable getMostGoalScorerTable() {
		return mostGoalScorerTable;
	}
	public void setMostGoalScorerTable(JTable mostGoalScorerTable) {
		this.mostGoalScorerTable = mostGoalScorerTable;
	}

	public JTextField getPlayerName() {
		return playerName;
	}

	public void setPlayerName(JTextField playerName) {
		this.playerName = playerName;
	}

	public JTextField getPlayerSurname() {
		return playerSurname;
	}

	public void setPlayerSurname(JTextField playerSurname) {
		this.playerSurname = playerSurname;
	}

	public JTable getPlayedMatchTable() {
		return playedMatchTable;
	}

	public void setPlayedMatchTable(JTable playedMatchTable) {
		this.playedMatchTable = playedMatchTable;
	}

	public JTable getPlayerGoalMatchTable() {
		return playerGoalMatchTable;
	}

	public void setPlayerGoalMatchTable(JTable playerGoalMatchTable) {
		this.playerGoalMatchTable = playerGoalMatchTable;
	}

	public JButton getGetPlayerStatisticButton() {
		return getPlayerStatisticButton;
	}

	public void setGetPlayerStatisticButton(JButton getPlayerStatisticButton) {
		this.getPlayerStatisticButton = getPlayerStatisticButton;
	}
	
}
